home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / prolog / sbprolog / amiga / v3_1 / sbp3_1e.lzh / FINDALL.PL < prev    next >
Text File  |  1991-10-31  |  1KB  |  42 lines

  1. /* From the book PROLOG PROGRAMMING IN DEPTH
  2.    by Michael A. Covington, Donald Nute, and Andre Vellino.
  3.    Copyright 1988 Scott, Foresman & Co.
  4.    Non-commercial distribution of this file is permitted. */
  5.  
  6. /* FINDALL.PL */
  7.  
  8. /*************************************************
  9.  * find_all(X,Goal,List)                         *
  10.  *  X contains one or more uninstantiated        *
  11.  *  variables that also occur in Goal.           *
  12.  *  (X can be a single uninstantiated variable.) *
  13.  *  List is a list of all of the instantiations  *
  14.  *  of X with which Goal will succeed.           *
  15.  *************************************************/
  16.  
  17. /* Note: This is a built-in predicate in Arity
  18.    and is called findall (no underscore mark). */
  19.  
  20. find_all(X,Goal,_) :-
  21.   asserta(find_item('*MARK*')),
  22.   call(Goal),
  23.   asserta(find_item(X)),
  24.   fail.
  25.  
  26. find_all(_,_,List) :-
  27.   find_collect([],L),
  28.   !,
  29.   List = L.
  30.  
  31. find_collect(List1,List2) :-
  32.   find_next(X),
  33.   !,
  34.   find_collect([X|List1],List2).
  35.  
  36. find_collect(List,List).
  37.  
  38. find_next(X) :-
  39.   retract(find_item(X)),
  40.   !,
  41.   X \== '*MARK*'.
  42.